package sniff.diller;

import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import sniff.pack.MyUDPPacket;
import util.ByteUtil;
import util.InfoUtil;

public class UDPPacketDiller {
	public static MyUDPPacket dill(Packet pack) {
		byte[] head = pack.header;
		int startIndex=34;
		if(((IPPacket) pack).version == 4) {
			startIndex=34;
		}else if(((IPPacket) pack).version == 6){
			startIndex=54;
		}else {
			System.out.println("!!!!!!!!!!!!!!!error UDPPacketDiller");
			return null;
		}
		int sourctPort = ByteUtil.unionByte(head[startIndex], head[startIndex+1]);
		int destinationPort = ByteUtil.unionByte(head[startIndex+2], head[startIndex+3]);
		int udpLen = ByteUtil.unionByte(head[startIndex+4], head[startIndex+5]);
		String checkNum = "0x"+ByteUtil.byteToHexString(head[startIndex+6])+ByteUtil.byteToHexString(head[startIndex+7]);
		MyUDPPacket udpPack = new MyUDPPacket(sourctPort, destinationPort, udpLen, checkNum);
		udpPack.setInfo(InfoUtil.getUDPInfo(udpPack));
		return udpPack;
	}
}
